package com.qlylkj.lbk.ui.user.login

import android.app.Activity
import android.content.Intent
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.Observer
import com.forest.core.etx.*
import com.jeremyliao.liveeventbus.LiveEventBus
import com.qlylkj.lbk.R
import com.qlylkj.lbk.base.BaseVMActivity
import com.qlylkj.lbk.model.event.LiveEvent
import com.qlylkj.lbk.ui.user.password.FindPasswordActivity
import com.qlylkj.lbk.ui.user.register.RegisterActivity
import kotlinx.android.synthetic.main.activity_login.*

class LoginActivity: BaseVMActivity<LoginViewModel>() {

    companion object{
        const val REQUEST_CODE_REGISTER = 1001
    }


    private var passwordShow = false

    override fun providerVMClass() = LoginViewModel::class.java

    override fun setLayoutId() = R.layout.activity_login

    override fun initView() {
        phoneNumberEdit.addTextChangedListener { check() }
        passwordEdit.addTextChangedListener { check() }
        close.setOnClickListener(onClickListener)
        login.setOnClickListener(onClickListener)
        passwordStatusImage.setOnClickListener(onClickListener)
        toRegister.setOnClickListener(onClickListener)
        toForgetPassword.setOnClickListener(onClickListener)
        login.isClickable = false
    }

    override fun initData() {

    }

    override fun startObserve() {
        super.startObserve()
        mViewModel.apply {
            uiState.observe(this@LoginActivity, Observer { uiModel ->
                uiModel?.run {
                    if (loginSuccess) {
                        toast("登录成功")
                        LiveEventBus.get(LiveEvent.LOGIN_KEY).post(true)
                        finish()
                    }
                    showError?.let { err ->
                        toast(err)
                    }
                }
            })
        }
    }

    private val onClickListener = View.OnClickListener {
        when (it.id) {
            R.id.close -> finish()
            R.id.passwordStatusImage -> changePasswordStatus()
            R.id.login -> login()
            R.id.toRegister -> toRegister()
            R.id.toForgetPassword -> toForgetPassword()
        }
    }

    private fun check() {
        val check = phoneNumberEdit.isPhoneNumber() && passwordEdit.isPassword(8)
        login.isClickable = check
        login.isSelected = check
    }

    private fun changePasswordStatus() {
        if (passwordShow) {
            passwordEdit.transformationMethod = PasswordTransformationMethod.getInstance()
            passwordStatusImage.setImageResource(R.mipmap.ic_password_display)
        } else {
            passwordEdit.transformationMethod = HideReturnsTransformationMethod.getInstance()
            passwordStatusImage.setImageResource(R.mipmap.ic_password_show)
        }
        passwordShow = !passwordShow
    }

    private fun login() {
        mViewModel.login(phoneNumberEdit.toTrim(),passwordEdit.toTrim())
    }

    private fun toRegister(){
        startKtxActivityForResult<RegisterActivity>(requestCode = REQUEST_CODE_REGISTER)
    }

    private fun toForgetPassword() {
        startKtxActivity<FindPasswordActivity>()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (REQUEST_CODE_REGISTER == requestCode && resultCode == Activity.RESULT_OK) {
            LiveEventBus.get(LiveEvent.LOGIN_KEY).post(true)
            finish()
        }
    }

}